package cn.lixiaodan.hqlbuilder.ui;

import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JInternalFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

import org.jdesktop.application.Application;

import cn.lixiaodan.hqlbuilder.model.ClassModel;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class ClassInternalFrame extends JInternalFrame {
	private JSplitPane jSplitPane1;
	private JTextField jTextField1;
	private JScrollPane jScrollPane1;
	private JList jList1;
	private NewSingleFrameApplication app;
	private ClassModel classModel = null;

	public ClassInternalFrame(String clsname, NewSingleFrameApplication app) {
		this.classModel = new ClassModel(clsname);
		this.classModel.query();
		this.app = app;
		initGUI();
		initColumns();
	}

	public ClassModel getClassModel() {
		return classModel;
	}

	public ClassModel getSelectedClassModel() {
		ClassModel cm = new ClassModel();
		cm.setName(this.classModel.getName());
		cm.setShortName(this.classModel.getShortName());

		Object selected[] = this.jList1.getSelectedValues();
		for (int i = 0; i != selected.length; i++) {
			cm.getFields().add(selected[i].toString());
		}
		return cm;
	}

	public void setClassModel(ClassModel classModel) {
		this.classModel = classModel;
	}

	private void initColumns() {
		DefaultComboBoxModel model = (DefaultComboBoxModel) this.jList1.getModel();
		for (int i = 0; i != classModel.getFields().size(); i++)
			model.addElement(classModel.getFields().get(i));
	}

	private void initGUI() {
		try {
			{
				this.setPreferredSize(new java.awt.Dimension(203, 374));
				BoxLayout thisLayout = new BoxLayout(getContentPane(), javax.swing.BoxLayout.Y_AXIS);
				getContentPane().setLayout(thisLayout);
				this.setBounds(0, 0, 203, 374);
				this.setName("this");
				this.setResizable(true);
				this.setClosable(true);
				this.addInternalFrameListener(new InternalFrameAdapter() {
					public void internalFrameClosed(InternalFrameEvent evt) {
						thisInternalFrameClosed(evt);
					}
				});
				{
					jSplitPane1 = new JSplitPane();
					getContentPane().add(jSplitPane1);
					jSplitPane1.setOrientation(JSplitPane.VERTICAL_SPLIT);
					{
						jTextField1 = new JTextField();
						jSplitPane1.add(jTextField1, JSplitPane.TOP);
						jTextField1.setName("");
					}
					{
						jScrollPane1 = new JScrollPane();
						jSplitPane1.add(jScrollPane1, JSplitPane.BOTTOM);
						{
							ListModel jList1Model = new DefaultComboBoxModel(new String[] {});
							jList1 = new JList();
							jScrollPane1.setViewportView(jList1);
							jList1.setModel(jList1Model);
							jList1.setCellRenderer(new CheckListCellRenderer());
							jList1.setName("jList1");
							jList1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
							jList1.setSelectionModel(new DefaultListSelectionModel() {
								public void setSelectionInterval(int index0, int index1) {
									if (isSelectedIndex(index0))
										super.removeSelectionInterval(index0, index1);
									else
										super.addSelectionInterval(index0, index1);
								}
							});
						}
					}
				}
			}
			Application.getInstance().getContext().getResourceMap(getClass()).injectComponents(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void thisInternalFrameClosed(InternalFrameEvent evt) {
		app.ciflist.remove(this);
	}

}
